﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;

namespace _010_RemotingClientTCPLease
{
    class Program
    {
        static void Main(string[] args)
        {



                try
                {
                    BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
                    provider.TypeFilterLevel = TypeFilterLevel.Full;
                    // Creating the IDictionary to set the port on the channel instance.
                    IDictionary props = new Hashtable();
                    props["port"] = 0;
                    // Pass the properties for the port setting and the server provider in the server chain argument. (Client remains null here.)
                    TcpChannel channel = new TcpChannel(props, null, provider);

                    ChannelServices.RegisterChannel(channel, true);

                    ActivatedClientTypeEntry entry = new ActivatedClientTypeEntry(typeof(_010_RemotingServerTCPFileLease.RemotingServer), "tcp://localhost:9999");
                    RemotingConfiguration.RegisterActivatedClientType(entry);

                    _010_RemotingServerTCPFileLease.RemotingServer srv = new _010_RemotingServerTCPFileLease.RemotingServer();
                    _10_RemotingSharedAssembly.IServerInterface isrv = (_10_RemotingSharedAssembly.IServerInterface)srv;


                    //*
                    ClientSponsor cp = new ClientSponsor(TimeSpan.FromMinutes(1));
                    ILease lease = (ILease)RemotingServices.GetLifetimeService(srv);
                    lease.Register(cp);
                    //*/

                    Console.WriteLine("Enter array size:");
                    int size = Int32.Parse(Console.ReadLine());
                    if (size > 0)
                    {
                        _10_RemotingSharedAssembly.ServerRequest rq = new _10_RemotingSharedAssembly.ServerRequest();
                        rq.Array = new int[size];
                        for (int i = 0; i < size; i++)
                        {
                            Console.Write("Enter element {0}:", i);
                            rq.Array[i] = Int32.Parse(Console.ReadLine());
                        }
                        Console.Write("Sending request to server ...");
                        int rq_index = isrv.AddRequest(rq);
                        Console.WriteLine("{0}", rq_index);

                        rq = isrv.GetRequest(rq_index);
                        foreach (int a in rq.Array) Console.Write("{0} ", a);
                        Console.WriteLine();

                        Console.WriteLine("Processing request ...");
                        isrv.ProcessRequest(rq_index);
                        Console.WriteLine("Done");

                        _10_RemotingSharedAssembly.ServerResponse resp = isrv.GetResponse(rq_index);
                        foreach (int a in resp.Array) Console.Write("{0} ", a);
                        Console.WriteLine();
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }

                Console.ReadLine();
        }
    }
}
